using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using LanguageTranslator.UI.Commands;
using LanguageTranslator.UI.Helper;
using LanguageTranslator.UI.Models;
using LanguageTranslator.UI.Services;
using LanguageTranslator.UI.Views;

namespace LanguageTranslator.UI.ViewModels
{
    public class MainWindowViewModel : ViewModelBase, IMainWindowViewModel, INotifyPropertyChanged
    {
        private Language _selectedCountry1;
        private Language _selectedCountry2;
        private ILanguageTranslatorService _translateService;

        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindowViewModel(IView view)
            : this(new TranslateLanguageModel(), view, new LanguageTranslatorService())
        {
        }

        public MainWindowViewModel(ITranslateLanguageModel model, IView view, ILanguageTranslatorService translateService)
        {
            Model = model;
            View = view;

            _translateService = translateService;

            Countries1 = new ObservableCollection<Language>();
            Countries1 = new ObservableCollection<Language>(LanguageHelper.SetDefaultLanguage(Countries1.ToList()));

            Countries2 = new ObservableCollection<Language>();
            Countries2 = new ObservableCollection<Language>(LanguageHelper.SetDefaultLanguage(Countries2.ToList()));
        }

        private DelegateCommand _translateCommand;

        public ICommand TranslateCommand
        {
            get { return _translateCommand ?? (_translateCommand = new DelegateCommand(TranslateProcessing)); }
        }

        public ObservableCollection<Language> Countries1 { get; set; }

        public Language SelectedCountry1
        {
            get { return Countries1.ToList().Find(x => x.Equals(_selectedCountry1)); }
            set { _selectedCountry1 = value; }
        }

        public ObservableCollection<Language> Countries2 { get; set; }

        public Language SelectedCountry2
        {
            get { return Countries2.ToList().Find(x => x.Equals(_selectedCountry2)); }
            set { _selectedCountry2 = value; }
        }

        public string InputText { get; set; }

        private string _translateResult;

        public string TranslateResult
        {
            get { return _translateResult; }
            set
            {
                _translateResult = value;
                if (null != this.PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("TranslateResult"));
                }
            }
        }

        public void TranslateProcessing()
        {
            TranslateResult = ProcessInpuParametters();
        }

        private string ProcessInpuParametters()
        {
            string result;
            if (SelectedCountry1 != null && SelectedCountry2 != null)
            {
                var builder = new StringBuilder();
                builder.AppendFormat("{0}|{1}", SelectedCountry1.Value, SelectedCountry2.Value);
                result = _translateService.TranslateLanguage(InputText, builder.ToString()).TranslatedText;
            }
            else
            {
                result = "Please input enough values!!!";
            }

            return result;
        }
    }
}